package com.ihr360.insurance.controller.ledger;

import com.google.common.collect.Lists;
import com.ihr360.commons.context.session.Ihr360Session;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.excel.specification.CommonSpecification;
import com.ihr360.excel.validation.SpreadSheetValidator;
import com.ihr360.excel.validation.annonation.SpreadSheetValid;
import com.ihr360.excel.vo.CustomSpreadSheetValidateResult;
import com.ihr360.excel.vo.SpreadSheetSpecification;
import com.ihr360.excel.vo.SpreadSheetValidatorRequest;
import com.ihr360.insurance.service.MonthlyLedgerService;
import com.ihr360.utils.SIHFConstants;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-05-16 10:31:39.
 */
@Component
@SpreadSheetValid(code = "MonthlyLedger")
public class MonthlyLedgerImportValidator implements SpreadSheetValidator {

    private static final String PARAM_LEDGER_YEAR = "ledgerYear";
    private static final String PARAM_LEDGER_MONTH = "ledgerMonth";

    @Autowired
    private MonthlyLedgerService monthlyLedgerService;


    @Override
    public CustomSpreadSheetValidateResult customValidate(SpreadSheetValidatorRequest request) {

        Ihr360Session session = Ihr360SessionContextHolder.getSession();
        if (session == null || session.getUserInfo() == null
                || session.getUserInfo().getCompanyId() == null
                || session.getUserInfo().getUserId() == null) {
            ValidationUtils.reject(null, "【福利台账导入校验】请求参数无效");
        }

        Map<String, Object> extraParams = request.getExtraParams();
        if (extraParams == null
                || !(extraParams.get(PARAM_LEDGER_YEAR) instanceof Integer)
                || !(extraParams.get(PARAM_LEDGER_MONTH) instanceof Integer)) {
            ValidationUtils.reject(null, "【福利台账导入校验】请求参数无效");
        }
        return monthlyLedgerService.validateExcelData(
                session.getUserInfo().getCompanyId(),
                session.getUserInfo().getUserId(),
                (Integer) extraParams.get(PARAM_LEDGER_YEAR),
                (Integer) extraParams.get(PARAM_LEDGER_MONTH), request);
    }

    @Override
    public SpreadSheetSpecification commonSpecification(SpreadSheetValidatorRequest request) {
        SpreadSheetSpecification spreadSheetSpecification = new SpreadSheetSpecification();
        CommonSpecification commonSpecification = CommonSpecification.createCommonSpecification(true);

        List<List<String>> requiredHeaders = Lists.newArrayList();
        ArrayList<String> staffNameColumn = Lists.newArrayList(SIHFConstants.STAFF_NAME_CELL_NAME);
        requiredHeaders.add(staffNameColumn);
        ArrayList<String> mobileNoColumn = Lists.newArrayList(SIHFConstants.MOBLIE_NO_CELL_NAME);
        requiredHeaders.add(mobileNoColumn);
        commonSpecification.setHeaderColumnJudge(requiredHeaders);
        commonSpecification.setStaffNameAlias(staffNameColumn);
        commonSpecification.setMobileNoAlias(mobileNoColumn);

        spreadSheetSpecification.setCommonSpecification(commonSpecification);
        return spreadSheetSpecification;
    }
}
